<template>
    <a-drawer title="输入数据" :height="600" placement="top" v-if="open" v-model:visible="open">
        <a-steps :current="current">
            <a-step title="基本信息" />
            <a-step title="仪器串信息" />
            <a-step title="其它" description="" />
        </a-steps>
        <br>
        <BasicInfo ref="BasicInfoRef" @getCurrent="getCurrent" :schemesData="props.schemesData" :state="props.state"
            :reportQuery="reportQuery" v-if="current === 0">
        </BasicInfo>
        <InstrumentInfo ref="InstrumentInfoRef" @getCurrent="getCurrent" :schemesData="props.schemesData"
            :state="props.state" :reportQuery="reportQuery" v-if="current === 1"></InstrumentInfo>
        <OtherInfo ref="OtherInfoRef" @getCurrent="getCurrent" :schemesData="props.schemesData" :state="props.state"
            :reportQuery="reportQuery" v-if="current === 2">
        </OtherInfo>
        <a-divider />
    </a-drawer>
</template>
<script setup>
import { ref } from 'vue';
import BasicInfo from './BasicInfo.vue'
import OtherInfo from './OtherInfo.vue'
import InstrumentInfo from './InstrumentInfo.vue'
import useApp from "@/hooks";
import { updateOne, createOne } from "@/api/salvage/accidentAlert.js";
import { WellDatas } from '@/custom/salvage/constant/address.js'  //新建井判断
const { $, proxy } = useApp();
const emit = defineEmits(["getData", 'deliverData'])
const props = defineProps({
    state: String,
    schemesData: Object
})
const current = ref(0)
const BasicInfoRef = ref(null)
const OtherInfoRef = ref(null)
const InstrumentInfoRef = ref(null)
const reportQuery = ref({})
const open = ref(false)

watch(() => props.state, (newVal) => {
    console.log(newVal)
    current.value = 0
    if (newVal === '修改') {
        localStorage.setItem("state", '修改');
    } else if (newVal === '新建') {
        localStorage.setItem("state", '新建');
        reportQuery.value = {}
    } else if (newVal === '补充') {
        localStorage.setItem("state", '补充');
        // reportQuery.value = {}
    }
})

// 提交
function getCurrent(e) {
    console.log('getCurrent', e);
    if (e.page === 'submit' && (props.state === '新建' || props.state === '补充')) {
        reportQuery.value = { ...reportQuery.value, ...e };
        console.log('新建reportQuery', reportQuery.value);
        createOne(reportQuery.value).then(res => {
            open.value = false
            current.value = undefined
            $.showToast({
                title: '新建成功',
                icon: 'success'
            })
            console.log('createOne', res);
            emit('deliverData', e)
            WellDatas(res.id, res.wellId)
        })
    } else if (e.page === 'submit' && props.state === '修改') {
        reportQuery.value = { ...reportQuery.value, ...e };
        reportQuery.value.id = props.schemesData.id
        console.log('修改reportQuery', reportQuery.value);
        updateOne(reportQuery.value).then(res => {
            $.showToast({
                title: '修改成功',
                icon: 'success'
            })
            open.value = false
            current.value = undefined
            console.log('updateOne', res);
            emit('deliverData', res)
        })
    } else {
        console.log(reportQuery.value)
        current.value = e.page
        reportQuery.value = { ...reportQuery.value, ...e };
        delete e.page
    }
}
function openDrawer() {
    current.value = 0
    open.value = true;
}
defineExpose({
    openDrawer: openDrawer
})

</script>
<style scoped>
.steps-content {
    margin-top: 16px;
    border: 1px dashed #e9e9e9;
    border-radius: 6px;
    background-color: #fafafa;
    min-height: 200px;
    text-align: center;
    padding-top: 80px;
}

.steps-action {
    margin-top: 24px;
}

[data-theme='dark'] .steps-content {
    background-color: #2f2f2f;
    border: 1px dashed #404040;
}
</style>